package pt.isec.gpsgroup5.moneybox;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RatingBar;

public class NewObjective extends Activity {

	private DBAdapter db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_objective);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_new_objective, menu);
		return true;
	}

	public void onNewObjectiveSave(View v) {
		String name = null;
		double finalAmount = -1;
		float priority;
		int year = -1, month = -1, day = -1;
		int id;
		AlertDialog.Builder bdialog;

		db = new DBAdapter(this);
		db.open(this);

		try {

			EditText newObjectiveName = (EditText) findViewById(R.id.newObjectiveName);
			name = newObjectiveName.getText().toString();

			EditText newObjectiveFinalAmount = (EditText) findViewById(R.id.newObjectiveFinalAmount);
			finalAmount = Double.parseDouble(newObjectiveFinalAmount.getText()
					.toString());
			Log.i("teste", "final" + finalAmount);

			DatePicker dp = (DatePicker) findViewById(R.id.newObjectiveDeadLine);
			year = dp.getYear();
			month = dp.getMonth() + 1;
			day = dp.getDayOfMonth();

			RatingBar rbpriority = (RatingBar) findViewById(R.id.newObjectivePriority);
			priority = rbpriority.getRating();

			bdialog = new AlertDialog.Builder(this);
			if (!db.isNameRepeated(name)) {
				if (!checkValues(day, month, year, finalAmount)) {
					bdialog.setTitle("");
					bdialog.setMessage(R.string.invalidData);
					bdialog.setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
								}
							});
					AlertDialog dialog = bdialog.create();
					dialog.show();

				} else {

					id = db.getNextAvailableID();
					Objective obj = new Objective(id, name, finalAmount, 0,
							year, month, day, (int) priority, 1);
					Log.i("teste", obj.toString() + "");
					db.createObjective(obj);
					db.queueAll();

					bdialog.setTitle("");
					bdialog.setMessage(R.string.newObjectiveSuccess);
					bdialog.setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
								}
							});
					AlertDialog dialog = bdialog.create();
					dialog.show();

				}

			} else {

				bdialog.setTitle("");
				bdialog.setMessage(R.string.repeatedName);
				bdialog.setPositiveButton("Ok",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				AlertDialog dialog = bdialog.create();
				dialog.show();
			}
		} catch (Exception e) {
		} finally {
			if (db != null)
				db.close();
		}
	}

	public boolean checkValues(int day, int month, int year, double finalAmount) {

		Calendar calendar = Calendar.getInstance();

		int currentYear = calendar.get(Calendar.YEAR);
		if (year < currentYear || year > Objective.maxYear) {
			return false;
		}

		if (month < 1 || month > 12) {
			return false;
		}

		int maxDay = -1;
		if (year != -1 && month != -1) {
			calendar.set(year, month, 1);
			maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

			if (day < 1 || day > maxDay) {
				return false;
			}
		}

		if (finalAmount <= 0 || finalAmount > Objective.maxAmount) {
			return false;
		}
		return true;
	}

}